﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.Globalization;

using Tomers.WPF.Localization;
using XmlVisualizer.Controls;

namespace XmlVisualizer.Dialogs {
	/// <summary>
	/// Interaction logic for DialogSettings.xaml
	/// </summary>
	public partial class DialogSettings : Window {
		private MessageBoxResult returnValue = MessageBoxResult.Cancel;

		public DialogSettings() {
			InitializeComponent();
			LoadSettings();
		}

		private void LoadSettings() {
			// ENVIRONMENT

			// language
			String language = Properties.Settings.Default.language;

			cbLanguages.Items.Clear();
			// languages
			StringCollection languages = Properties.Settings.Default.languages;
			foreach (String lang in languages) {
				cbLanguages.Items.Add(lang);
			}
			cbLanguages.SelectedItem = language;

			// canvas background color
			Color canvasColor = Properties.Settings.Default.canvasBackgroundColor;
			recCanvasColor.Fill = new SolidColorBrush(canvasColor);

			// END ENVIRONMENT

			// XML

			// item's width
			Point itemWidth = Properties.Settings.Default.defaultSize;
			tbItemWidth.Text = itemWidth.X.ToString();

			// item's stroke color
			Color itemStrokeColor = Properties.Settings.Default.defaultStrokeColor;
			recItemStrokeColor.Fill = new SolidColorBrush(itemStrokeColor);

			// item's fill
			Color itemColor = Properties.Settings.Default.defaultFillColor;
			recItemBgColor.Fill = new SolidColorBrush(itemColor);

			// item's appearance
			String shape = Properties.Settings.Default.defaultAppearance;
			Style ctrl = (Style)FindResource(shape.Substring(10));
			currentAppearance.Style = ctrl;
			//currentAppearance.ApplyTemplate();

			// END XML
		}

		public MessageBoxResult ShowModal() {
			ShowDialog();
			return returnValue;
		}

		private void btnApply_Click(object sender, RoutedEventArgs e) {
			SaveSettings();
			LoadSettings();
		}

		private void btnOK_Click(object sender, RoutedEventArgs e) {
			SaveSettings();
			Close();
		}

		private void SaveSettings() {
			// ENVIRONMENT

			// language
			String selectedLanguage = cbLanguages.SelectedItem as String;
			Properties.Settings.Default.language = selectedLanguage;
			LanguageContext.Instance.Culture = CultureInfo.GetCultureInfo(Properties.Settings.Default.language);
			
			// canvas background color
			SolidColorBrush brush = recCanvasColor.Fill as SolidColorBrush;
			Color canvasColor = brush.Color;
			Properties.Settings.Default.canvasBackgroundColor = canvasColor;
			
			// END ENVIRONMENT

			// XML

			// item's width
			Point itemWidth = new Point(Double.Parse(tbItemWidth.Text), 0);
			Properties.Settings.Default.defaultSize = itemWidth;

			// item's stroke color
			SolidColorBrush itemStrokeBrush = recItemStrokeColor.Fill as SolidColorBrush;
			Color itemStrokeColor = itemStrokeBrush.Color;
			Properties.Settings.Default.defaultStrokeColor = itemStrokeColor;

			// item's fill
			SolidColorBrush itemBrush = recItemBgColor.Fill as SolidColorBrush;
			Color itemColor = itemBrush.Color;
			Properties.Settings.Default.defaultFillColor = itemColor;

			// item's appearance
			if (border_Appearances.Child is ComboBox) {
				ComboBox cb = border_Appearances.Child as ComboBox;
				object selectedItem = cb.SelectedItem;
				if (selectedItem is ComboBoxItem) {
					ComboBoxItem selectedComboBoxItem = selectedItem as ComboBoxItem;
					string appearance = selectedComboBoxItem.Tag as String;
					Properties.Settings.Default.defaultAppearance = appearance;
				}
			}
			
			// END XML

			Properties.Settings.Default.Save();
		}

		private void btnCanvasColor_Click(object sender, RoutedEventArgs e) {
			Color color = ColorPicker.PickColor(((SolidColorBrush)recCanvasColor.Fill).Color);
			recCanvasColor.Fill = new SolidColorBrush(color);
		}

		//private Color PickColor() {
		//    ColorPickerDialog cPicker = new ColorPickerDialog();
		//    cPicker.StartingColor = Colors.Black;

		//    Color fillColor = cPicker.StartingColor;
		//    bool? dialogResult = cPicker.ShowDialog();
		//    if (dialogResult != null && (bool)dialogResult == true) {
		//        fillColor = cPicker.SelectedColor;
		//    }
		//    return fillColor;
		//}

		private void btnBgColor_Click(object sender, RoutedEventArgs e) {
			Color color = ColorPicker.PickColor(((SolidColorBrush)recItemBgColor.Fill).Color);
			recItemBgColor.Fill = new SolidColorBrush(color);
		}

		private void btnStrokeColor_Click(object sender, RoutedEventArgs e) {
			Color color = ColorPicker.PickColor(((SolidColorBrush)recItemStrokeColor.Fill).Color);
			recItemStrokeColor.Fill = new SolidColorBrush(color);
		}
	}
}
